# *******************************************************************************
# Copyright (c) 2019 UT-Battelle, LLC.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# and Eclipse Distribution License v.10 which accompany this distribution.
# The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
# and the Eclipse Distribution License is available at
# https://eclipse.org/org/documents/edl-v10.php
#
# Contributors:
#   Alexander J. McCaskey - initial API and implementation
# *******************************************************************************/
add_subdirectory(tpls/legacy-sapi-clients/c-client)

set(LIBRARY_NAME xacc-dwave)

set(ANTLR_LIB ${CMAKE_SOURCE_DIR}/dist/libantlr4-runtime.so)
if (APPLE)
  set(ANTLR_LIB ${CMAKE_SOURCE_DIR}/dist/libantlr4-runtime.dylib)
endif()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-format-security")
# dwave_sapi uses std::random_shuffle which is deprecated since C++17
set_property(TARGET dwave_sapi PROPERTY CXX_STANDARD 11)
file(GLOB SRC
          accelerator/DWave.cpp
          DWaveActivator.cpp
          embedding/CMREmbedding.cpp
          generators/rbm.cpp)

usfunctiongetresourcesource(TARGET ${LIBRARY_NAME} OUT SRC)
usfunctiongeneratebundleinit(TARGET ${LIBRARY_NAME} OUT SRC)

add_library(${LIBRARY_NAME} SHARED ${SRC})

target_include_directories(${LIBRARY_NAME}
                           PUBLIC accelerator
                                  compiler
                                  compiler/generated
                                  embedding
                                  generators
                                  embedding/minorminer/include
                                  tpls/legacy-sapi-clients/c-client/include
                                  tpls/legacy-sapi-clients/find-embedding/include
                                  tpls/legacy-sapi-clients/c-client/include
                                  tpls/legacy-sapi-clients/remote/include
                                  ${CMAKE_SOURCE_DIR}/tpls/rapidjson/include
                                  ${CMAKE_SOURCE_DIR}/tpls/antlr/runtime/src)


target_link_libraries(${LIBRARY_NAME}
                      PUBLIC xacc
                             xacc-quantum-annealing
                             xacc-quantum-gate
                             ${ANTLR_LIB} dwave_sapi
                             )

set(_bundle_name xacc_dwave)
set_target_properties(${LIBRARY_NAME}
                      PROPERTIES COMPILE_DEFINITIONS
                                 US_BUNDLE_NAME=${_bundle_name}
                                 US_BUNDLE_NAME
                                 ${_bundle_name})

usfunctionembedresources(TARGET
                         ${LIBRARY_NAME}
                         WORKING_DIRECTORY
                         ${CMAKE_CURRENT_SOURCE_DIR}
                         FILES
                         manifest.json)

if(APPLE)
  set_target_properties(${LIBRARY_NAME}
                        PROPERTIES INSTALL_RPATH "@loader_path/../lib")
  set_target_properties(${LIBRARY_NAME}
                        PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
else()
  set_target_properties(${LIBRARY_NAME}
                        PROPERTIES INSTALL_RPATH "$ORIGIN/../lib")
  set_target_properties(${LIBRARY_NAME} PROPERTIES LINK_FLAGS "-shared")
endif()

if(XACC_BUILD_TESTS)
  add_subdirectory(tests)
  add_subdirectory(generators/tests)
endif()

install(TARGETS ${LIBRARY_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/plugins)

#file(GLOB PYDECORATORS ${CMAKE_CURRENT_SOURCE_DIR}/decorators/*.py)
#install(FILES ${PYDECORATORS} DESTINATION ${CMAKE_INSTALL_PREFIX}/py-plugins)

#add_subdirectory(ir)